package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.KnowledgeType;
import ywpt.service.config.KnowledgeTypeService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class KnowledgeTypeAction extends BaseAction implements ModelDriven<KnowledgeType>, Preparable {

	/**
	 * 知识点类别Action
	 */
	private static final long serialVersionUID = -690423753691025745L;
	private static final int DEFAULT_PAGE_SIZE = 100;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	private Long id;
	private Long parentId;
	private KnowledgeType knowledgeType;
	private List<KnowledgeType> knowledgeTypeList = new ArrayList<KnowledgeType>();
	private KnowledgeTypeService knowledgeTypeService;
	
	
	public boolean[] getPopedom() {
		return popedom;
	}
	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public KnowledgeType getKnowledgeType() {
		return knowledgeType;
	}
	public void setKnowledgeType(KnowledgeType knowledgeType) {
		this.knowledgeType = knowledgeType;
	}
	public List<KnowledgeType> getKnowledgeTypeList() {
		return knowledgeTypeList;
	}
	public void setKnowledgeTypeList(List<KnowledgeType> knowledgeTypeList) {
		this.knowledgeTypeList = knowledgeTypeList;
	}
	public KnowledgeTypeService getKnowledgeTypeService() {
		return knowledgeTypeService;
	}
	public void setKnowledgeTypeService(KnowledgeTypeService knowledgeTypeService) {
		this.knowledgeTypeService = knowledgeTypeService;
	}
	public Long getParentId() {
		return parentId;
	}
	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
	/*---------------------------------------------------------------------------------------------*/
	@Override
	public KnowledgeType getModel() {
		return this.knowledgeType;
	}
	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.knowledgeType = this.knowledgeTypeService.getKnowledgeTypeById(id);
		}else{
			this.knowledgeType = new KnowledgeType();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.CONFIG_KNOW_TYPE);
	}
	/*==============================================================================================*/
	
	public String list() throws Exception {
		this.knowledgeTypeList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(KnowledgeType.class);
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows= knowledgeTypeService.getKnowledgeTypeCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),totalRows,
				DEFAULT_PAGE_SIZE,0);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		knowledgeTypeList.addAll(knowledgeTypeService.getKnowledgeTypes(query, pageSize, rowStartEnd[0]));		
		return SUCCESS;
	}
	
	public String input(){
		String typeCode = this.knowledgeTypeService.creatKnowledgeTypeCode(null);
		this.knowledgeType.setTypeCode(typeCode);
		return INPUT;
	}	
	
	public String edit(){
		this.knowledgeType = this.knowledgeTypeService.getKnowledgeTypeById(id);
		if(knowledgeType.getParent()!=null)
			this.parentId = knowledgeType.getParent().getId();
		return INPUT;
	}	
	
	public String save() throws ServletException{
		try
		{
			if(id==null){
				knowledgeTypeService.saveKnowledgeType(knowledgeType,parentId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			}else{
					knowledgeTypeService.updateKnowledgeType(knowledgeType,parentId);
					this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}			
		}	
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL+e.getMessage());
			return SUCCESS;
		}
		return SUCCESS;
	}
	
	public String delete()
	{
		if(id!=null)
		{
			try
			{	
				knowledgeTypeService.removeKnowledgeType(id);
				
			}
			catch(UniqueConstraintException e)
			{
				this.addActionMessage("删除失败！"+e.getMessage());
				return INPUT;
			}
		}
		else
		{
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
		return SUCCESS;
	}
	public String select(){
		DetachedCriteria query = DetachedCriteria.forClass(KnowledgeType.class);
		if(this.parentId!=null)
			query.createAlias("parent", "parent").add(Restrictions.eq("parent.id", parentId));
		else 
			query.add(Restrictions.isNull("parent"));
		this.knowledgeTypeList.addAll(knowledgeTypeService.getKnowledgeTypes(query));
		return SUCCESS;
	}

	
}
